import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    patients: [],
    selectedPatientId: null,
    selectedStudyId: null
  },
  
  mutations: {
    SET_PATIENTS(state, patients) {
      state.patients = patients
    },
    
    SELECT_PATIENT(state, patientId) {
      state.selectedPatientId = patientId
    },
    
    SELECT_STUDY(state, studyId) {
      state.selectedStudyId = studyId
    }
  },
  
  actions: {
    setPatients({ commit }, patients) {
      commit('SET_PATIENTS', patients)
    },
    
    selectPatient({ commit }, patientId) {
      commit('SELECT_PATIENT', patientId)
    },
    
    selectStudy({ commit }, studyId) {
      commit('SELECT_STUDY', studyId)
    }
  },
  
  getters: {
    getSelectedPatient(state) {
      return state.patients.find(patient => patient.id === state.selectedPatientId)
    }
  }
}) 